package com.study.servlet;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

/**
 * 自定义 HttpServlet：基于注解配置
 *
 * @author Administrator
 */
@Slf4j
@WebServlet(name = "testServletAnnotation", urlPatterns = "/test/anno/servlet.do")
public class TestServletAnnotation extends HttpServlet {
    @Override
    public void init() throws ServletException {
        log.info("TestServletAnnotation init()......");
        super.init();
    }

    @Override
    public void destroy() {
        log.info("TestServletAnnotation destroy()......");
        super.destroy();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doService(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doService(request, response);
    }

    private void doService(HttpServletRequest request, HttpServletResponse response) {
        log.info("TestServletAnnotation 业务处理开始...");
        // 操作 attribute
        request.setAttribute("a", "a");
        request.setAttribute("a", "b");
        request.getAttribute("a");
        request.removeAttribute("a");
        // 操作 session
        request.getSession().setAttribute("a", "a");
        request.getSession().getAttribute("a");
        request.getSession().invalidate();
        log.info("TestServletAnnotation 业务处理结束...");
    }
}
